﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;

namespace Logistics
{
    public partial class frmRolesModify : Form
    {
        public delegate void ModelEventHandler(Model.tc_Role model);
        public ModelEventHandler OnModelEvent;
        private Model.tc_Role Role;
        public frmRolesModify()
        {
            InitializeComponent();
        }
        public frmRolesModify(Model.tc_Role _role)
        {
            InitializeComponent();
            Role = _role;
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            if (!VerifyFrom())
                return;
            var model = new Model.tc_Role();
            model.Role = txtName.Text.Trim();
            var msg = "确定要保存'" + txtName.Text + "'吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                var objRole = new Manager.tc_Role();
                if (Role == null)
                {
                    model.Id = Guid.NewGuid();
                    objRole.Add(model);
                }
                else
                {
                    model.Id = Role.Id;
                    objRole.Update(model);
                }
                OnModelEvent?.Invoke(model);
                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
        private bool VerifyFrom()
        {
            var msg = string.Empty;
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                msg = "角色名称不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }
            return true;
        }
        private void butCancel_Click(object sender, EventArgs e)
        {
            var msg = "数据尚未保存，确认要取消吗？";
            if (MessageBox.Show(msg, "警告", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void frmRolesModify_Load(object sender, EventArgs e)
        {
            if (Role != null)
                DataBind();
        }
        private void DataBind()
        {
            txtName.Text = Role.Role;
        }
    }
}
